#!/bin/bash

# Replace with your container name
CONTAINER_NAME="oracle19c"

# Function to check if a docker container is running
is_container_running() {
    container_name=$1
    if [ "$(docker ps -q -f name=^/${container_name}$)" ]; then
        return 0
    else
        return 1
    fi
}

# Step 1: Check for Docker
if ! command -v docker &> /dev/null; then
    echo "Docker is not installed. Please install Docker and try again."
    exit 1
fi

# Step 2: Check for Docker Compose
if ! command -v docker-compose &> /dev/null; then
    echo "Docker Compose is not installed. Please install Docker Compose and try again."
    exit 1
fi

# Step 3: Check if specified Docker container is running
if is_container_running $CONTAINER_NAME; then
    echo "Docker container '$CONTAINER_NAME' is already running."
    exit 1
else
    echo "Docker container '$CONTAINER_NAME' is not running. Proceeding..."
fi

# Step 4: Start the application using Docker Compose
echo "Starting the application with Docker Compose..."
mkdir data && chmod 777 data
docker run --ulimit nofile=1024 -d --name oracle19c \
    -p 1521:1521 \
    -e ORACLE_SID=orcl \
    -e ORACLE_PWD=Hzsun@310012 \
    -e ORACLE_CHARACTERSET=AL32UTF8 \
    -v `pwd`/data:/opt/oracle/oradata \
    registry.cn-hangzhou.aliyuncs.com/zyzbp/oracle-19c:19.3

echo "Oracle19c is starting, please wait a moment."

# Print the current server IP address
IP_ADDRESS=$(hostname -I | awk '{print $1}')

echo "================================================"
echo "'$CONTAINER_NAME' 连接信息"
echo "Oracle Host: ${IP_ADDRESS}"
echo "Oracle Port: 1521"
echo "Oracle User: system"
echo "Oracle Pwd : Hzsun@310012"
echo "Oracle SID : orcl"
echo "================================================"
echo ""
echo "执行 docker logs -f oracle19c 查看安装日志信息"
